package com.ciko.code.base;

import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciko.code.http.BaseRequest;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.HttpClient;
import com.ciko.code.http.HttpResponseListener;
import com.ciko.code.utils.LogUtils;
import com.ciko.code.utils.ToastUtils;
import com.mywork.R;
import com.squareup.picasso.Picasso;

/**
 * Created by 12154 on 2015/10/27.
 */
public abstract class BaseFragment extends Fragment implements View.OnClickListener, HttpResponseListener {

    private final String TAG = this.getClass().getSimpleName();

    protected LinearLayout rootLayout;

    private View common_nav;

    private View rect_imgNavBack;
    private View rect_imgNavRight;

    private TextView tv_title;

    private ViewStub common_nav_left;
    private ViewStub common_nav_right;
    private ViewStub common_nav_title;

    private FrameLayout fl_content;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        rootLayout = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.layout_root_base, null);

        common_nav = rootLayout.findViewById(R.id.common_nav);

        common_nav_left = (ViewStub) rootLayout.findViewById(R.id.common_nav_left);
        common_nav_right = (ViewStub) rootLayout.findViewById(R.id.common_nav_right);
        common_nav_title = (ViewStub) rootLayout.findViewById(R.id.common_nav_title);

        fl_content = (FrameLayout) rootLayout.findViewById(R.id.fl_content);

        initTopBar();
        initLayout();
        initView();
        initAdapter();
        initListener();
        initDates();
        initHttpRequest();

        return rootLayout;
    }

    protected abstract void initTopBar();

    protected abstract void initLayout();

    protected abstract void initView();

    protected abstract void initAdapter();

    protected abstract void initDates();

    protected abstract void initHttpRequest();

    protected abstract void initListener();

    protected <T extends View> T findViewById(int id) {
        return (T) fl_content.findViewById(id);
    }

    public void setContentView(int layoutResID) {
        LogUtils.v(TAG, "setContentView()");
        fl_content.addView(LayoutInflater.from(getContext()).inflate(layoutResID, null), ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
    }

    protected void unableNav() {
        common_nav.setVisibility(View.GONE);
    }

    public void enableNavLeftImg() {
        LogUtils.v(TAG, "enableNavLeftImg()");

        common_nav_left.setLayoutResource(R.layout.layout_nav_left_img);
        rect_imgNavBack = common_nav_left.inflate();
        rect_imgNavBack.setOnClickListener(this);
    }

    public void enableNavRightImg(int imgResID) {
        LogUtils.v(TAG, "enableNavRightImg()");

        common_nav_right.setLayoutResource(R.layout.layout_nav_right_img);
        rect_imgNavRight = common_nav_right.inflate();
        ImageView img = (ImageView) rect_imgNavRight.findViewById(R.id.iv_imgNavRight);
        img.setImageResource(imgResID);
        rect_imgNavRight.setOnClickListener(this);
    }

    public void setTitle(CharSequence title) {
        LogUtils.v(TAG, "setTitle()");

        common_nav_title.setLayoutResource(R.layout.layout_nav_title);
        tv_title = (TextView) common_nav_title.inflate();
        tv_title.setText(title);
    }

    protected void showToast(CharSequence msg) {
        LogUtils.v(TAG, "showToast()");
        ToastUtils.showShort(msg);
    }

    protected void intent2Acticity(Class<?> cls) {
        Intent intent = new Intent(getActivity(), cls);
        startActivity(intent);
    }

    protected void logV(String msg) {
        LogUtils.v(TAG, msg);
    }

    protected void logV(int msg) {
        LogUtils.v(TAG, msg + "");
    }

    protected void logD(String msg) {
        LogUtils.d(TAG, msg);
    }

    protected void logD(int msg) {
        LogUtils.d(TAG, msg + "");
    }

    protected void logI(String msg) {
        LogUtils.i(TAG, msg);
    }

    protected void logI(int msg) {
        LogUtils.i(TAG, msg + "");
    }

    protected void logW(String msg) {
        LogUtils.w(TAG, msg);
    }

    protected void logW(int msg) {
        LogUtils.w(TAG, msg + "");
    }

    protected void logE(String msg) {
        LogUtils.e(TAG, msg);
    }

    protected void logE(int msg) {
        LogUtils.e(TAG, msg + "");
    }

    @Override
    public void onClick(View v) {
        LogUtils.v(TAG, "onClick()");

        if(v == rect_imgNavBack){
            showToast("点击了左边");
        } else if(v == rect_imgNavRight){
            showToast("点击了右边");
        }

//        switch (v.getId()) {
//            case R.id.rect_imgNavBack:
//                showToast("点击了左边");
//                break;
//            case R.id.rect_imgNavRight:
//                showToast("点击了右边");
//                break;
//        }
    }

    public Context getContext() {
        return getActivity();
    }

    @Override
    public void onRequestStart(String tag) {

    }

    @Override
    public void onResponseSuccess(BaseResponse<?> response, String tag) {

    }

    @Override
    public void onResponseFail(String tag,int resultCode) {

    }

    public void loadImageUrl(ImageView imageView, String imgUrl) {
        Picasso.with(getContext()).load(imgUrl).placeholder(R.drawable.shape_rect_gray).into(imageView);
    }

    protected void doRequestHttp(BaseRequest request) {
        HttpClient.getInstance(getActivity()).post(request, this);
    }

}
